package org.front;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.dao.OrderDao;
import org.dao.ProductDao;
import org.dao.UserDao;
import org.entity.Order;
import org.entity.Product;
import org.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


@Scope("prototype")
@Component("WishAction")
public class WishAction extends ActionSupport {
	private static final long serialVersionUID = -9167213418951271794L;
	private Map<String, Object> dataMap = new HashMap<String, Object>();
	private Product product;
	private String id;
	private int count;
	@Autowired
	private ProductDao productDao;
	@Autowired
	private OrderDao entityDao;
	@Autowired
	private UserDao userDao;
	
	private String postname,posttel,postadd;
	
	public String wlist(){
		ActionContext context = ActionContext.getContext();
		
		dataMap.clear();
		double zj=0;
		if(context.getSession().get("WISHLIST")!=null){
			List<Order> orders=(List<Order>)context.getSession().get("WISHLIST");
			for(Order order :orders){
				zj=zj+Double.parseDouble(order.getPosttel());
			}	
		}
		
		context.put("zj", zj);
		return "wlist";
	}
	
	public void addwishlist(){
		dataMap.clear();
		
		ActionContext context = ActionContext.getContext();
		User loginUser=(User)context.getSession().get("userEntity");
		if(loginUser==null){
			dataMap.put("status", true);
			dataMap.put("message", "你还没有登录,请先登陆后再选购商品!");
			return;
		}
		List<Order> orders=new ArrayList<Order>();
		Product productitem=productDao.findById(product.getId());
		if(context.getSession().get("WISHLIST")!=null){
			orders=(List<Order>)context.getSession().get("WISHLIST");
			Order order = new Order();
			order.setId(UUID.randomUUID().toString());
			//价格
			order.setExt3(productitem.getExt2());
			//单位
			order.setExt2(productitem.getSize());
			order.setExt1(productitem.getFilepath());
			order.setUserid(loginUser.getId());
			order.setUsername(loginUser.getUsername());
			order.setProductid(productitem.getId());
			order.setProductname(productitem.getName());
			order.setCount(""+product.getExt1());
			
			//比例
			String ext1=loginUser.getExt1();
			//Product product = productDao.findById(entity.getProductid());
			//价格
			String ext2=productitem.getExt2();
			//数量
			String count =product.getExt1();
			
			double price=(Integer.parseInt(count)*Integer.parseInt(ext2))*Double.parseDouble(ext1);
			
			order.setPosttel(price+"");
			
			orders.add(order);
		}else{
			Order order = new Order();
			order.setId(UUID.randomUUID().toString());
			order.setExt3(productitem.getExt2());
			order.setExt2(productitem.getSize());
			order.setExt1(productitem.getFilepath());
			order.setUserid(loginUser.getId());
			order.setUsername(loginUser.getUsername());
			order.setProductid(productitem.getId());
			order.setProductname(productitem.getName());
			order.setCount(""+product.getExt1());
			
			//比例
			String ext1=loginUser.getExt1();
			//Product product = productDao.findById(entity.getProductid());
			//价格
			String ext2=productitem.getExt2();
			//数量
			String count =product.getExt1();
			
			double price=(Integer.parseInt(count)*Integer.parseInt(ext2))*Double.parseDouble(ext1);
			
			order.setPosttel(price+"");
			orders.add(order);
		}
		double zj=0;
		for(Order order :orders){
			zj=zj+Double.parseDouble(order.getPosttel());
		}
		context.put("zj", zj);
		context.getSession().put("WISHLIST",orders);
		dataMap.put("message", "加入购入车成功！");
		dataMap.put("status", true);
	}
	
	public String removewishlist(){
		ActionContext context = ActionContext.getContext();
		List<Order> orders=(List<Order>)context.getSession().get("WISHLIST");
		dataMap.clear();
		for(Order item:orders){
			if(id.equals(item.getId())){
				orders.remove(item);
				break;
			}
		}
		dataMap.clear();
		double zj=0;
		for(Order order :orders){
			zj=zj+Double.parseDouble(order.getPosttel());
		}
		context.put("zj", zj);
		return "delwlist";
	}
	public String addOrder(){
		ActionContext context = ActionContext.getContext();
		if(context.getSession().get("WISHLIST")!=null){
			List<Order> orders=(List<Order>)context.getSession().get("WISHLIST");
			for(Order item:orders){
				item.setExt3(item.getPosttel());
				item.setExt2("发货");
				item.setPostadd(postadd);
				item.setPostname(postname);
				item.setPosttel(posttel);
				
				item.setId(null);
				entityDao.update(item);
			}
			context.getSession().put("WISHLIST",null);
		}
		
		return "delwlist";
	}
	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPostname() {
		return postname;
	}

	public void setPostname(String postname) {
		this.postname = postname;
	}

	public String getPosttel() {
		return posttel;
	}

	public void setPosttel(String posttel) {
		this.posttel = posttel;
	}

	public String getPostadd() {
		return postadd;
	}

	public void setPostadd(String postadd) {
		this.postadd = postadd;
	}
	
}
